%%%-------------------------------------------------------------------
%%% File    : valg_SUITE.erl
%%% Author  :  <vjache>
%%% Description : 
%%%
%%% Created : 16 Apr 2009 by  <vjache>
%%%-------------------------------------------------------------------
-module(valg_SUITE).

%% Note: This directive should only be used in test suites.
-compile(export_all).

-include("../include/valg.hrl").
-include("../include/test.hrl").
-include_library("ct.hrl").

%%--------------------------------------------------------------------
%% COMMON TEST CALLBACK FUNCTIONS
%%--------------------------------------------------------------------

%%--------------------------------------------------------------------
%% Function: suite() -> Info
%%
%% Info = [tuple()]
%%   List of key/value pairs.
%%
%% Description: Returns list of tuples to set default properties
%%              for the suite.
%%
%% Note: The suite/0 function is only meant to be used to return
%% default data values, not perform any other operations.
%%--------------------------------------------------------------------
suite() ->
    [{timetrap,{minutes,10}}].

%%--------------------------------------------------------------------
%% Function: init_per_suite(Config0) ->
%%               Config1 | {skip,Reason} | {skip_and_save,Reason,Config1}
%%
%% Config0 = Config1 = [tuple()]
%%   A list of key/value pairs, holding the test case configuration.
%% Reason = term()
%%   The reason for skipping the suite.
%%
%% Description: Initialization before the suite.
%%
%% Note: This function is free to add any key/value pairs to the Config
%% variable, but should NOT alter/remove any existing entries.
%%--------------------------------------------------------------------
init_per_suite(Config) ->
	ct:log(default1,"Load test data."),
 	load_ages_hobbies(Config),
	ct:log(default1,"Test data loaded."),
    Config.

%%--------------------------------------------------------------------
%% Function: end_per_suite(Config0) -> void() | {save_config,Config1}
%%
%% Config0 = Config1 = [tuple()]
%%   A list of key/value pairs, holding the test case configuration.
%%
%% Description: Cleanup after the suite.
%%--------------------------------------------------------------------
end_per_suite(_Config) ->
    ok.

%%--------------------------------------------------------------------
%% Function: init_per_testcase(TestCase, Config0) ->
%%               Config1 | {skip,Reason} | {skip_and_save,Reason,Config1}
%%
%% TestCase = atom()
%%   Name of the test case that is about to run.
%% Config0 = Config1 = [tuple()]
%%   A list of key/value pairs, holding the test case configuration.
%% Reason = term()
%%   The reason for skipping the test case.
%%
%% Description: Initialization before each test case.
%%
%% Note: This function is free to add any key/value pairs to the Config
%% variable, but should NOT alter/remove any existing entries.
%%--------------------------------------------------------------------
init_per_testcase(_TestCase, Config) ->
    Config.

%%--------------------------------------------------------------------
%% Function: end_per_testcase(TestCase, Config0) ->
%%               void() | {save_config,Config1}
%%
%% TestCase = atom()
%%   Name of the test case that is finished.
%% Config0 = Config1 = [tuple()]
%%   A list of key/value pairs, holding the test case configuration.
%%
%% Description: Cleanup after each test case.
%%--------------------------------------------------------------------
end_per_testcase(_TestCase, _Config) ->
    ok.

%%--------------------------------------------------------------------
%% Function: sequences() -> Sequences
%%
%% Sequences = [{SeqName,TestCases}]
%% SeqName = atom()
%%   Name of a sequence.
%% TestCases = [atom()]
%%   List of test cases that are part of the sequence
%%
%% Description: Specifies test case sequences.
%%--------------------------------------------------------------------
sequences() -> 
    [].

%%--------------------------------------------------------------------
%% Function: all() -> TestCases | {skip,Reason}
%%
%% TestCases = [TestCase | {sequence,SeqName}]
%% TestCase = atom()
%%   Name of a test case.
%% SeqName = atom()
%%   Name of a test case sequence.
%% Reason = term()
%%   The reason for skipping all test cases.
%%
%% Description: Returns the list of test cases that are to be executed.
%%--------------------------------------------------------------------
all() -> 
    [
	 map_rs,
	 j2mv_oc,
	 key_fold_rs].


%%--------------------------------------------------------------------
%% TEST CASES
%%--------------------------------------------------------------------

%%--------------------------------------------------------------------
%% Function: TestCase() -> Info
%%
%% Info = [tuple()]
%%   List of key/value pairs.
%%
%% Description: Test case info function - returns list of tuples to set
%%              properties for the test case.
%%
%% Note: This function is only meant to be used to return a list of
%% values, not perform any other operations.
%%--------------------------------------------------------------------
map_rs() -> 
    [].

%%--------------------------------------------------------------------
%% Function: TestCase(Config0) ->
%%               ok | exit() | {skip,Reason} | {comment,Comment} |
%%               {save_config,Config1} | {skip_and_save,Reason,Config1}
%%
%% Config0 = Config1 = [tuple()]
%%   A list of key/value pairs, holding the test case configuration.
%% Reason = term()
%%   The reason for skipping the test case.
%% Comment = term()
%%   A comment about the test case that will be printed in the html log.
%%
%% Description: Test case function. (The name of it must be specified in
%%              the all/0 list for the test case to be executed).
%%--------------------------------------------------------------------
map_rs(_Config) -> 
	V1=vect_mnesia:new_mem_bag(ages1, [fname_lname,age]),
	Rs={?RS_TAG,iter:from_list([
									 {"Vasya","Ivanov",23},
									 {"Dima","Pupkin",27},
									 {"Masha","Bublikova",32},
									 {"Katya","Shkvarkina",19},
									 {"Yulya","Sharikova",34},
									 {"Vasya","Bukin",14},
									 {"Ostap","Bender",36},
									 {"Yuriy","Dolgorukiy",44},
									 {"Chingiz","Han",50}
									])},
	MapExpr1 = {?MAP_TAG, 
				Rs , 
				fun({Fn,Ln,Age}=_V)-> {ages1,{Fn,Ln},Age} end},
	Pg=valg:eval(MapExpr1, pipe_vect:new(V1)),
	ct:log(default,lists:flatten(valg:explain(Pg))),
    ok.
%%--------------------------------------------------------------------
%% Function: TestCase() -> Info
%%
%% Info = [tuple()]
%%   List of key/value pairs.
%%
%% Description: Test case info function - returns list of tuples to set
%%              properties for the test case.
%%
%% Note: This function is only meant to be used to return a list of
%% values, not perform any other operations.
%%--------------------------------------------------------------------
j2mv_oc() -> 
    [].
j2mv_oc(_Config) ->
	V1=vect_mnesia:new_mem_bag(ages, [id,age]),
	V2=vect_mnesia:new_mem_bag(hobbies, [id,hobby]),
	%V3=vect_mnesia:new_mem_bag(j1, [fname,lname,age,hobby]),
	MapExpr1 = {?VECT_TAG,V1},
	MapExpr2 = {?VECT_TAG,V2},
	JoinExpr = {?JOIN_TAG,
				[MapExpr1,MapExpr2],
				fun([{ages,{Fn,Ln},Age},{hobbies,{Fn,Ln},Hobby}])-> 
						{j1,Fn,Ln,Age,Hobby};
				   (X) ->
						exit({bag_out_format,X})
				end },
	Pg=valg:eval(JoinExpr, pipe_console:new()),
	ct:log(default,lists:flatten(valg:explain(Pg))),
	util:wait_finish(pgt:new(Pg)),
    ok.

key_fold_rs() -> 
    [].
key_fold_rs(Config) -> 
    ok.
%%--------------------------------------------------------------------
%% Helper functions
%%--------------------------------------------------------------------
load_ages_hobbies(Config) ->
	Pid1=util:map_from_file(?CONFIG(data_dir,Config)++"/ages.tup",vect_mnesia:new_mem_bag(ages, [id,age]),
				  fun({X,Y,Z})->
						  {ages,{X,Y},Z}
				  end),
	Pid2=util:map_from_file(?CONFIG(data_dir,Config)++"/hobbies.tup",vect_mnesia:new_mem_bag(hobbies, [id,hobby]),
				  fun({X,Y,Z})->
						  {hobbies,{X,Y},Z}
				  end),
	util:wait_finish([Pid1,Pid2]).
